package com.metro.demo.excal.utils;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;

public class test2 {
    @Test
    public void test(){
        Long start = System.currentTimeMillis();
        createXml();
        System.out.println("运行时间："+ (System.currentTimeMillis() - start));
    }

    /**
     * 生成xml方法
     */
    public static void createXml(){
        try {
            // 1、创建document对象
            Document document = DocumentHelper.createDocument();
            // 2、创建根节点workbook
            Element workbook = document.addElement("workbook");
            Element worksheet = workbook.addElement("worksheet");
            // 3、向workbook节点添加idx属性
            worksheet.addAttribute("idx", "0");
            // 4、生成子节点section
            Element section = worksheet.addElement("section");
            section.addAttribute("idx", "0");


            Element loop = worksheet.addElement("loop");
            loop.addAttribute("startRow","3");
            loop.addAttribute("endRow","3");
            loop.addAttribute("items","biz4sinfos");
            loop.addAttribute("var","item");
            loop.addAttribute("varType","com.metro.demo.biz.beans.vo.Biz4sinfo");


            Element section2 = loop.addElement("section");
            section2.addAttribute("startRow","3");
            section2.addAttribute("endRow","3");


            for (int i=0; i<10; i++){
                Element mapping = section2.addElement("mapping");
                mapping.addAttribute("row","3");
                mapping.addAttribute("caol",String.valueOf(i));
                mapping.setText("item.cord"+i);
            }

            Element loopbreakcondition = loop.addElement("loopbreakcondition");
            Element rowcheck = loopbreakcondition.addElement("rowcheck");
            rowcheck.addAttribute("offset","0");

            Element cellcheck = rowcheck.addElement("cellcheck");
            cellcheck.addAttribute("offset","0");


            // 5、设置生成xml的格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            // 设置编码格式
            format.setEncoding("UTF-8");

            // 6、生成xml文件
            File file = new File("rss.xml");
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            // 设置是否转义，默认使用转义字符
            writer.setEscapeText(false);
            writer.write(document);
            writer.close();
            System.out.println("生成rss.xml成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("生成rss.xml失败");
        }
    }
}
